package org.ewaf.framework.struts2.result;

import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;

public class JSONResult implements Result {

	private static final long serialVersionUID = -5560202412199072402L;
	private static final Logger log = Logger.getLogger(JSONResult.class);
	private String contentType;
	private String encoding = "UTF-8"; // JSON结果串编码方式
	private String expression = "jsonResult"; // 处于值栈中的待显示成JSON格式的对象,默认为：jsonResult
	private String excludes;// 配置过滤的属性,既不住客户端发送的的数据

	@Override
	public void execute(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletResponse response = (HttpServletResponse) actionContext
				.get(StrutsStatics.HTTP_RESPONSE);

		try {
			Object jsonObject = null;
			ValueStack stack = invocation.getStack();
			Object object = stack.findValue(getExpression());
			if (object != null) {
				if (object instanceof Collection<?>
						|| object.getClass().isArray()) {
					if (StringUtils.isNotBlank(excludes)) {
						JsonConfig config = new JsonConfig();
						config.setExcludes(excludes.split(","));
						jsonObject = JSONArray.fromObject(object, config);
					} else {
						jsonObject = JSONArray.fromObject(object);
					}

				} else {
					if (StringUtils.isNotBlank(excludes)) {
						JsonConfig config = new JsonConfig();
						config.setExcludes(excludes.split(","));
						jsonObject = JSONObject.fromObject(object, config);
					} else {
						jsonObject = JSONObject.fromObject(object);
					}

				}
			}
			response.setContentType(getContentType());
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			if (jsonObject != null) {
				response.getWriter().write(jsonObject.toString());
			} else {
				response.getWriter().write("");
			}
		} catch (Exception exception) {
			log.error(exception.getMessage(), exception);
			throw exception;
		}
	}

	public String getContentType() {
		
		if(StringUtils.isBlank(contentType)){
			
			return "text/javascript" + ";charset=" + getEncoding();
		}
		
		
		return contentType;
		
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	public String getExcludes() {
		return excludes;
	}

	public void setExcludes(String excludes) {
		this.excludes = excludes;
	}

}
